package cn.tedu.hongbeifang.web;

import cn.tedu.hongbeifang.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
//@Accessors(chain = true)
public class JsonResult<T> implements Serializable {

    /**
     * 操作结果的状态码（状态标识）
     */
    @ApiModelProperty("业务状态码")
    private Integer state;

    /**
     * 操作失败时的提示文本
     */
    @ApiModelProperty("提示文本")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;

    /**
     * 操作成功返回的数据
     */
    @ApiModelProperty("数据")
    private T data;

    public static JsonResult<Void> ok() {
        return JsonResult.ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<T>();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.data=data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e) {

        return fail(e.getCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode state, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = state.getValue();
        jsonResult.message = message;
        return jsonResult;
    }

}
